<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        const hireTypEnmu= {1:'正式',2:'非正式'}

        // 思考如何将list中的数据转化为最终输出的结果？？？
        const list = [
            {
                correctionTime: "2018-11-30",
                departmentName: "总裁办",
                formOfEmployment: 1,
                id: "604f764971f93f3ac8f365c2",
                mobile: "13800000002",
                password: "e10adc3949ba59abbe56e057f20f883e",
                staffPhoto: "http://q6cu3t6jv.bkt.clouddn.com/1063705989926227968?t=1616204161907",
                timeOfEntry: "2018-11-02",
                username: "管理员",
                workNumber: "9002",
            },
            {
                correctionTime: "2018-11-20",
                departmentName: "市场部",
                formOfEmployment: 1,
                id: "604f764971f93f3ac8f365c3",
                mobile: "13800000003",
                password: "e10adc3949ba59abbe56e057f20f883e",
                staffPhoto: "http://q6cu3t6jv.bkt.clouddn.com/1063705989926227968?t=1616204161907",
                timeOfEntry: "2018-11-04",
                username: "孙财",
                workNumber: "111",
            }
        ]

        function formatData(list){
            const mapInfo = {
               'id': '编号',
               'password': '密码',
               'mobile': '手机号',
               'username': '姓名',
               'timeOfEntry': '入职日期',
               'formOfEmployment': '聘用形式',
               'correctionTime': '转正日期',
               'workNumber': '工号',
               'departmentName': '部门',
               'staffPhoto': '头像地址'
           }
           let header = []// 表头
           let data = [] // 存储数据值
         const one = list[0]
         if(!one){
            return { header, data }
         }
         header = Object.keys(one).map(key =>mapInfo[key])

        data =   list.map(obj => {
            obj.formOfEmployment = hireTypEnmu[obj.formOfEmployment] // 转换正式非正式
          return  Object.values(obj)
         })
         return {header,data}
        }

    //    function formatData(list) {
    //         const map = {
    //             'id': '编号',
    //             'password': '密码',
    //             'mobile': '手机号',
    //             'username': '姓名',
    //             'timeOfEntry': '入职日期',
    //             'formOfEmployment': '聘用形式',
    //             'correctionTime': '转正日期',
    //             'workNumber': '工号',
    //             'departmentName': '部门',
    //             'staffPhoto': '头像地址'
    //         }
    //         let header = []
    //         // header = ['id', 'mobile', 'username', .....]
    //         // data = [
    //         //     ['65c2', '1380000002', '管理员', ....],
    //         //     ['65c3', '1380000003', '孙财', ....],
    //         // ]
    //         let data = []
    //         // 开始代码
    //         // 找到一个元素
    //         const one = list[0]
    //         if (!one) {
    //             return { header, data }
    //         }
    //         header = Object.keys(one).map(key => {
    //             return map[key]
    //         })

    //         // data把list中每一个对象转成 对应的value数组
    //         data = list.map(obj => {
    //             // 把  Obj['formOfEmployment']: 1 , 2   ---> '正式'， '非正式'
    //             const key = obj['formOfEmployment'] // 1, 2
    //             obj['formOfEmployment'] = hireTypEnmu[key] // hireTypEnmu:{1:'正式', '2':'非正式' }

    //             return Object.values(obj)
    //         })

    //         return { header, data }
    //     }

    //     const {header,data} = formatData(list)
    //     console.log(header);
    //     console.log(data);
    </script>

<body>

</body>

</html>